package cn.mily.pet.service.impl;

import cn.mily.Start;
import cn.mily.basics.utils.DistanceUtil;
import cn.mily.basics.utils.PageBean;
import cn.mily.basics.utils.Point;
import cn.mily.org.domain.Employee;
import cn.mily.org.domain.Shop;
import cn.mily.org.mapper.EmployeeMapper;
import cn.mily.org.mapper.ShopMapper;
import cn.mily.org.query.ShopQuery;
import cn.mily.pet.domain.SearchMasterMsg;
import cn.mily.pet.mapper.SearchMapper;
import cn.mily.pet.query.FindMastQuery;
import cn.mily.pet.service.ISearchService;
import cn.mily.user.domain.LoginInfo;
import cn.mily.user.domain.User;
import cn.mily.user.mapper.UserMapper;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SearchServiceImpl implements ISearchService {

    @Autowired
    private SearchMapper mapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 发布寻主
     * @param searchMasterMsg
     */
    @Override
    public void searchMasterMsg(SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        /*获取请求中的token*/
        String token = request.getHeader("token");
        /*根据token在redis中获取登录信息*/
        RedisTemplate redisTemplate = Start.getRedisTemplate();
        LoginInfo loginInfo = (LoginInfo) redisTemplate.opsForValue().get(token);
        /*根据登录信息获取id  根据id 获取user*/
        User user = userMapper.findByInfoId(loginInfo.getId());
        /*根据距离选择最近的门店*/
        /*获取当前用户选择的位置经纬度*/
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        /*与全部的店铺地址比较*/
        ShopQuery query = new ShopQuery();
        Integer count = shopMapper.getCount(query);
        query.setPageSize(count);
        Shop shop = DistanceUtil.getNearestShop(point, shopMapper.findAll(query));
        /*封装*/
        searchMasterMsg.setShop(shop);
        searchMasterMsg.setUser(user);
        mapper.searchMasterMsg(searchMasterMsg);
    }

    /**
     *  查询 + 列表展示
     * @param query
     * @return
     */
    @Override
    public PageBean<SearchMasterMsg> findAll(FindMastQuery query,HttpServletRequest request) {
        /*获取请求中的token*/
        String token = request.getHeader("token");
        /*根据token在redis中获取登录信息*/
        RedisTemplate redisTemplate = Start.getRedisTemplate();
        LoginInfo loginInfo = (LoginInfo) redisTemplate.opsForValue().get(token);
        /*根据登录id 查找员工shop id*/
        Employee employee=employeeMapper.findByLoginId(loginInfo.getId());
        query.setShopid(employee.getShop_id());
        Integer count = mapper.getCount(query);/*查询结果数量*/
        if(count==null||count<=0){/*数量为0就直接退出*/
            return null;
        }
        PageBean<SearchMasterMsg> pageBean = new PageBean<>();
        pageBean.setTotal(count);/*将数量和数据封装*/
        pageBean.setData(mapper.findAll(query));
        return pageBean;
    }
}
